package com.aisile.shop.controller;


import com.aisile.entity.PageResult;
import com.aisile.entity.Result;
import com.aisile.pojo.TbGoods;
import com.aisile.pojogroup.Goods;
import com.aisile.sellergoods.service.GoodsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("goods")
public class GoodsController {

    @Reference
    GoodsService goodsService;

    @RequestMapping("save")
    public Result save(@RequestBody Goods goods){

        String sellerId= SecurityContextHolder.getContext().getAuthentication().getName();
        goods.getGoods().setSellerId(sellerId);
            try {
                if(!StringUtils.isEmpty(sellerId)) {
                    goodsService.save(goods);

                    return  new Result(true,"成功");
                }
                    return new Result(false,"请先登陆商家端");
            }catch (Exception e){

                return new Result(false,"服务器出现异常");

            }
        }

        @RequestMapping("findPage")
        public PageResult findPage(int page, int rows, @RequestBody TbGoods tbGoods){

            String sellerId=SecurityContextHolder.getContext().getAuthentication().getName();

            tbGoods.setSellerId(sellerId);

            return goodsService.findPage(page,rows,tbGoods);
        }
        @RequestMapping("findOne")
        public Goods findOne(long id){

            return goodsService.findOne(id);

        }

    }

